package com.example.demo.thread;

import com.example.demo.service.DZDPScratchService;
import com.example.demo.service.impl.DZDPScratchImpl;
import org.jsoup.HttpStatusException;

import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author ZhengWei
 * @Description
 * @date 2020/7/15 9:58 上午
 */

public class SkuTask implements Runnable {
    private DZDPScratchService scratchService;

    private int cityId;

    private AtomicInteger totalSku;

    private CountDownLatch countDownLatch;

    public SkuTask(DZDPScratchService scratchService, int cityId, AtomicInteger totalSku, CountDownLatch countDownLatch) {
        this.cityId = cityId;
        this.scratchService = scratchService;
        this.totalSku = totalSku;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        int skuId = totalSku.getAndDecrement();
        while (skuId > 0) {
            scratchService.scratchSku(cityId, skuId);
            System.out.println(Thread.currentThread() + " 剩余任务数：" + totalSku.get());
            skuId = totalSku.getAndDecrement();
        }
        countDownLatch.countDown();
    }
}